package com.evan.webadmin.biz.controller;


import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.util.MyPageUtil;
import com.evan.webadmin.biz.dto.EvluateNewDtoFilter;
import com.evan.webadmin.biz.dto.EvluateRecordNewDto;
import com.evan.webadmin.biz.model.EvluateSurvey;
import com.evan.webadmin.biz.service.IEvluateRecordService;
import com.evan.webadmin.biz.service.IEvluateSurveyService;
import com.evan.webadmin.biz.vo.EvluateRecordVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2025-03-11
 */
@RestController
@RequestMapping("/admin/biz/evluate/record")
public class EvluateRecordController {

    @Autowired
    private IEvluateRecordService recordService;
    @Autowired
    private IEvluateSurveyService surveyService;

    @PostMapping("/save")
    public ResponseResult<EvluateRecordNewDto> record(@RequestBody EvluateRecordNewDto frontDto, HttpServletRequest request) {
        return recordService.record(frontDto, request);
    }

    @PostMapping("/exist")
    public ResponseResult<EvluateRecordNewDto> exist(@RequestBody EvluateNewDtoFilter filter, HttpServletRequest request) {
        return recordService.exist(filter, request);
    }

    @PostMapping("/rankList")
    public ResponseResult<MyPageData<EvluateRecordVO>> rankList(@MyRequestBody EvluateNewDtoFilter filter,
                                                             @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        if(filter.getSurveyId() == null){
            EvluateSurvey survey = surveyService.findPublishedPaper();
            if(survey != null){
                filter.setSurveyId(survey.getId());
            }
        }
        List<EvluateRecordVO> examRankVos = recordService.rankList(filter.getSurveyId());
        long totalCount = 0L;
        if (examRankVos instanceof Page) {
            totalCount = ((Page<EvluateRecordVO>) examRankVos).getTotal();
        }
        return ResponseResult.success(MyPageUtil.makeResponseData(examRankVos, totalCount));
    }
}

